package cn.phantom.juctest;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

class BankCount {

    String bankName = "ccb";

    public volatile int money = 0;

    AtomicIntegerFieldUpdater fieldUpdater = AtomicIntegerFieldUpdater.newUpdater(BankCount.class,  "money");

    public void transfer(BankCount bankCount) {
        fieldUpdater.incrementAndGet(bankCount);

    }
}


public class AtomicIntegerFieldUpdaterDemo {
    public static void main(String[] args) {

        BankCount bankCount = new BankCount();
        for (int i = 0; i < 1000 ; i++) {
            new Thread(() -> {
                bankCount.transfer(bankCount);
            }, String.valueOf(i)).start();
        }

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + " \t" + "bankCount: " + bankCount.money);
    }
}
